const execSync = require('child_process').execSync;
const fs = require('fs');

try {
  const d = new Date();
  const commitId = execSync(`git rev-parse HEAD`).toString().trim();
  const branch = execSync(`git rev-parse --abbrev-ref HEAD`).toString().trim();
  const release = execSync(`git describe --always`).toString().trim();
  const version = {
    COMMIT_ID: commitId,
    Branch: branch,
    Release: release,
    buildTime: `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')} ${d
      .getHours()
      .toString()
      .padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`
  };
  const jsonContent = JSON.stringify(version, null, 2);
  fs.writeFileSync(`dist/version.json`, jsonContent);
} catch (e) {
  throw new Error(e);
}
